package app.view.mediators
{
	import app.AppConstants;
	import app.model.flare.proxies.FlareDataProxy;
	import app.model.flare.vos.StateVO;
	import app.view.component.FlareViz;
	
	import flare.vis.data.Data;
	import flare.vis.data.EdgeSprite;
	import flare.vis.data.NodeSprite;
	import flare.vis.events.SelectionEvent;
	import flare.vis.events.VisualizationEvent;
	
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.core.DragSource;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class FlareVizMediator extends Mediator
	{
		public static const NAME:String = "FlareVizMediator"
		private var isMouseOverNode:Boolean;
		
		public function FlareVizMediator(viewComponent:Object=null){
			super(NAME, viewComponent);
		}		
		
		public function get interactive():Boolean{
  			return view.interactive;
  		}
  		
  		public function set interactive(val:Boolean):void{
  			view.interactive = val;  			
  		}
  		
  		public function deselect():void{
  			view.deselect();
  		}
  		
  		public function set selectedState( state:StateVO):void{
  			view.selectedState = state;
  		}
		
		override public function onRegister():void{
			view.addEventListener(SelectionEvent.DESELECT, onDeselect);
			view.addEventListener(SelectionEvent.SELECT, onSelect);
			view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			view.addEventListener(VisualizationEvent.UPDATE, onUpdate);
		}
		
		private function onDeselect( event:SelectionEvent ):void{
			if(event.node){
				isMouseOverNode = false;
				sendNodeDeselectNote(event.node);
			}
			if(event.edge)sendEdgeDeselectNote(event.edge);
		}
		
		private function onSelect( event:SelectionEvent ):void{
			if(event.node)onNodeSelect(event);
			if(event.edge)onEdgeSelect(event);
			
		}
		
		private function onUpdate( event:VisualizationEvent ):void{
			sendVisualisationUpdated();
			
		}
		
		
		
		private function onNodeSelect(event:SelectionEvent):void{
			var type:String = event.cause.type,
				node:NodeSprite = event.node;
			switch(true){
					
				case (type == MouseEvent.MOUSE_OVER ):
					isMouseOverNode = true;
					sendNodeRollOverNote( node );
					break;
					
				case (type == MouseEvent.CLICK ):	
					sendNodeClickNote( node );
					break;
					
				
			}
		}
		
		private function onMouseDown(event:MouseEvent):void{
			var node:NodeSprite;
			node = event.target as NodeSprite;
			if(!node || !event.shiftKey)return;
			
			startDrag(node, event);
		}
		
		private function startDrag( node:NodeSprite, event:MouseEvent ):void{
			var dragSource:DragSource = new DragSource(),
				imageProxy:Canvas = new Canvas();
			dragSource.addData(node.data, "dragTarget");               
			imageProxy.height=16;
 			imageProxy.width=16; 
 			imageProxy.setStyle("backgroundColor", 0xff0000)           
    		DragManager.doDrag(view, dragSource, event, 
                    imageProxy, -view.mouseX, -view.mouseY);
             
           
		}
		
		private function onEdgeSelect(event:SelectionEvent):void{
			var type:String = event.cause.type,
				edge:EdgeSprite = event.edge;
			switch(true){
				
				case (type == MouseEvent.CLICK ):	
					sendEdgeClickNote( edge );
					break;
					
				case (type == MouseEvent.DOUBLE_CLICK ):	
					break;
					
				case (type == MouseEvent.MOUSE_OVER ):	
					sendEdgeRollOverNote( edge );
					break;
			}
		}
		
		
		private function sendNodeClickNote(node:NodeSprite):void{
			sendNotification(AppConstants.NODE_CLICK, node );
		}
		
		private function sendNodeRollOverNote(node:NodeSprite):void{
			sendNotification(AppConstants.NODE_ROLL_OVER, node );
		}
		
		private function sendEdgeClickNote(edge:EdgeSprite):void{
			sendNotification(AppConstants.EDGE_CLICK, edge );
		}
		
		private function sendEdgeRollOverNote(edge:EdgeSprite):void{
			sendNotification(AppConstants.EDGE_ROLL_OVER, edge );
		}
		
		private function sendNodeDeselectNote(node:NodeSprite):void{
			sendNotification(AppConstants.NODE_ROLL_OUT, node );
		}
		
		private function sendEdgeDeselectNote(edge:EdgeSprite):void{
			sendNotification(AppConstants.EDGE_ROLL_OUT, edge );
		}
		
		private function sendVisualisationUpdated():void{
			sendNotification(AppConstants.VISUALISATION_UPDATED, this );
		}
		
		private function sendSelectionNote( type:String, node:NodeSprite):void{
			switch(type){
				case MouseEvent.CLICK:	
					sendNodeClickNote( node )
					break;
					
				default:	
					sendNodeRollOverNote( node );
					break;
			}
		}
		
		override public function listNotificationInterests():Array{
			return [	FlareDataProxy.FLARE_DATA_POPULATED,
						FlareDataProxy.DATA_FLUSHED
															]
		}
		
		override public function handleNotification(note:INotification):void{
			
			switch( note.getName() ){
				
				case FlareDataProxy.FLARE_DATA_POPULATED:
					view.dataProvider = Data( note.getBody() );
					break;
					
				case FlareDataProxy.DATA_FLUSHED:
					view.flush();
					break;
					
			
				
			}
		}
		
		
		
			
		private function get view():FlareViz{
			return FlareViz(viewComponent);
		}
		
	}
}